 **
 **                ADMINISTRACAO
 **

local mcabeca[8],mestrutura[8]
 para empresa
 if pcount() = 1
    mtipo = 1
 else 
    mtipo = 0
 endif
 mretencao := rvalor := 0
 sele notasa
 go top
 while .not. eof()
  sele notasa
  mcliente = cliente
  mcusto   = '   '
  sele clientes
  seek mcliente
  if .not. eof()
     mcusto = ccusto1 
  endif
  sele notasa
  if alteracao()
     repla ccusto1 with mcusto
     unlock
  endif
  sele notasa
  skip
 enddo 
 sele notasa  
 set filter to empresa = mtipo
 go bottom
 skip -10
 limpa(3,0,24,80,cor,.f.)
 limpa(3,0,23,79,cor,.t.)
 mcabeca[1] = 'NOTA'        ; mestrutura[1] = fiel(1)
 mcabeca[2] = 'EMISSAO'     ; mestrutura[2] = fiel(3)
 mcabeca[3] = 'VENCTO'      ; mestrutura[3] = fiel(4)
 mcabeca[4] = 'RECBTO'      ; mestrutura[4] = fiel(21)
 mcabeca[5] = 'TOTAL'       ; mestrutura[5] = fiel(20)
 mcabeca[6] = 'I'           ; mestrutura[6] = fiel(19)
 mcabeca[7] = 'CCC'         ; mestrutura[7] = fiel(28)
 mcabeca[8] = 'EP'          ; mestrutura[8] = fiel(30)
 menu(24,0,' Inclui ',2,1)
 menu(24,8,'Altera ',1,1)
 menu(24,15,'Exclui ',1,1)
 menu(24,22,'Procura ',1,1)
 menu(24,30,'Boleto ',1,1)
 menu(24,37,'Relatorios ',1,1)
 menu(24,48,'GPS ',1,1)
 menu(24,52,'Recebimento ',3,1)
 menu(24,64,'Nota ',3,1)
 menu(24,72,'Esc-Sai ',1,3)
 dbedit(4,1,22,78,mestrutura,'admini100','@X',mcabeca,.t.)
 sele notasa
 set filter to
retu


function admini100(modo,coluna)
set cursor on
mcliente = cliente
@ 3,0 say space(78) + str(mtipo,2) color 'w+/r'
sele clientes
seek mcliente
if .not. eof()
  @ 3,0 say ' Cliente : ' + str(mcliente,6) + ' ' + nome color 'w+/r'
endif
sele notasa

if modo = 0 .or. modo = 3
   retu(1)
endif
tecla = lastkey()
do case
   case tecla = 5 .and. modo = 1

    mensa(10,'Inicio do arquivo ',2)
    retu(2)

   case tecla = 24 .and. modo = 2

    mensa(10,' Final do arquivo ',2)
    retu(2)

   case tecla = 27

    msaida = .t.
    retu(0)


   case tecla = 73 .or. tecla = 105  
                          
        sele 100
        use item
        xinss      = right(dtoc(date()),7)
        telageral := savescreen(0,0,24,80)
        sele notasa
        while .t.
         legal(0,0,24,79,'Inclusao')
         sele notasa 
         go bottom
         mnumero = numero+1
         @ 1,2  say 'N.F. Numero   :' get mnumero pict '999999'
         read
         if empty(mnumero) .or. lastkey() = 27
            exit
         endif
         mtemp = str(mnumero,6) + str(mtipo,2)
         seek mtemp  
         if .not. eof()
            mensa(10,'Nota Fiscal ja cadastrada',2)
            loop
         endif
         inicializa()
         minss    = xinss
         mcliente = 0
         @ 2,2 say 'Cliente       :' get mcliente  pict '999999'
         read
         sele clientes
         seek mcliente
         if eof()
            telalocal = savescreen(0,0,24,80)
            do clientes with .t.
            mcliente = codigo
            restscreen(0,0,24,80,telalocal)
         endif
         mirrf   = ir
         mcsll   = cs
         mpis    = pis
         mcofins = cofins
         mccusto  = ccusto
         mccusto1 = ccusto1
         @ 2,2  say 'Cliente       : ' + strzero(mcliente,6) + ' ' + left(nome,50)
         sele notasa
         @ 1,50 say 'Competencia   :' get minss     pict '99/9999'
         @ 3,2  say 'Emissao       :' get memissao
         @ 3,40 say 'Vencimento    :' get mvencto  
         read
         mlinha     = 3
         mmodelo    = 0
         maliqiss   = 5
         legal(10,4,14,30,'Busca NF Modelo')
         sele item 
         zap
         @ 12,6 say 'Nota :' get mmodelo pict '999999'
         read
         if mtipo = 0
            mcontra = 1
         else
            mcontra = 0
         endif
         if .not. empty(mmodelo)
            sele itemadmi
            mtemp   = str(mmodelo,6) + str(mtipo,2)
            seek mtemp  
            mtemp = mtipo
            if eof()
               mtemp = str(mmodelo,6) + str(mcontra,2)
               seek mtemp
               if eof()
                  mensa(10,'Itens da nota fiscal nao encontrados',2)
                  loop
               endif
               mtemp = mcontra
            endif
            while nota = mmodelo .and. empresa = mtemp
             mdescricao = descricao
             munitario  = unitario
             mvalor     = valor
             mtiposerv  = tiposerv
             sele item
             locate for (descricao = mdescricao .and. unitario = munitario .and. valor = mvalor)
             if eof()
                appe blank
                repla descricao with mdescricao
                repla unitario  with munitario
                repla valor     with mvalor
                repla tiposerv  with mtiposerv
             endif
             sele itemadmi
             skip
            enddo

            mtemp = str(mmodelo,6) + str(mtipo,2)
            sele notasa
            seek mtemp  
            if eof()
               set filter to
               locate for numero = mmodelo
               if eof()
                  mensa(10,'Nota fiscal de modelo nao encontrada !',2)
               endif
               mretencao1 = retencao1
               mretencao2 = retencao2
               mretencao3 = retencao3  
               mbanco     = banco
               mmaterial  = material
               set filter to empresa = mtipo
            else 
                mretencao1 = retencao1
                mretencao2 = retencao2
                mretencao3 = retencao3  
                mbanco     = banco
                mmaterial  = material
            endif
         endif
         telalocal = savescreen(0,0,24,80)
         sele item
         go top
         declare cabeca[3], estrutura[3]
         cabeca[1]  = 'Detalhamento' ; estrutura[1] = field(1)
         cabeca[2]  = 'Unitario'     ; estrutura[2] = field(2)
         cabeca[3]  = 'Total'        ; estrutura[3] = field(3)
         menu(24,0,' Incluir ',2,1)
         menu(24,9,'Alterar ',1,1)
         menu(24,17,'Excluir ',1,1)
         menu(24,72,'Esc-Sai ',1,3)
         dbedit(2,1,23,78,estrutura,'itensnf','@!',cabeca,.T.)
         mtotal = 0
         go top
         while .not. eof()
          mtotal += valor         
          skip   
         enddo
         restscreen(0,0,24,80,telalocal)
         @ 16,2  say 'Total da Nota Fiscal ' + space(40) + trans(mtotal,'9,999,999.99') color 'w+/r'
         @ 17,2  say 'Transporte :' get mvaletransp pict '999,999.99'
         @ 17,28 say 'Refeicao :'   get mvalealimen pict '999,999.99'
         @ 17,54 say 'Material :'   get mvmaterial   pict '999,999.99'
         read
         teste(18,2,mretencao1)
         mretencao1 = mretencao
         mvalorret1 = rvalor
         teste(19,2,mretencao2)
         mretencao2 = mretencao
         mvalorret2 = rvalor
         teste(20,2,mretencao3)
         mretencao3 = mretencao
         mvalorret3 = rvalor
         sele notasa
         @ 21,02 say 'IRRF : ' + trans(mirrf,'99.99')
         @ 21,15 say 'CSLL : ' + trans(mcsll,'99.99')
         @ 21,28 say 'COFINS : ' + trans(mcofins,'99.99')
         @ 21,43 say 'PIS : ' + trans(mpis,'99.99')
         mtemp      = mirrf + mcsll + mpis + mcofins
         mfederal   = mtotal * (mtemp / 100)
         @ 21,57 say  trans(mtemp,'99.99') + trans(mfederal,'99,999,999.99')
         mliquido = mtotal - (mvalorret1 + mvalorret2 + mvalorret3 + mfederal)
         @ 22,2  say 'Total liquido ' + space(48) + trans(mliquido,'9,999,999.99') color 'w+/r'
         @ 23,2  say 'Banco      :' get mbanco pict '99'
         @ 23,20 say 'Material :' get mmaterial pict '!' valid mmaterial $ 'SN'
         read
         sele bancos
         seek mbanco
         if eof()                     
            telabanco = savescreen(0,0,24,80)
            do bancos with .t.
            mbanco = codigo
            restscreen(0,0,24,80,telabanco)
         endif
         @ 24,20 say alltrim(nome) + ' Agencia : ' + alltrim(agencia) + ' C/C : ' + alltrim(conta)
         sele notasa
         @ 23,36 say 'Aliq ISS :' get maliqiss pict '99'
         read
         if mretencao1 # 3 .and. mretencao2 # 3 .and. mretencao3 # 3
            miss = mtotal * (maliqiss/100)
            @ 23,55 say 'I.S.S.   : ' + trans(miss,'999,999.99')
         endif
         if confirma(4,60,' 1 - Confirma ',' 2 - Cancela  ')
            appe blank
            if inclusao()
               grava()
               repla empresa with mtipo
               unlock
               sele itemadmi
               go top
               while .not. eof()
                if mnumero = nota .and. empresa = mtipo
                   if alteracao()
                      dele
                      unlock
                   endif
                endif
                skip
               enddo
               sele item
               go top
               while .not. eof()
                 sele item
                 mdescricao = descricao
                 munitario  = unitario
                 mvalor     = valor
                 mtiposerv  = tiposerv
                 sele itemadmi
                 appe blank
                 repla nota      with mnumero
                 repla descricao with mdescricao    
                 repla unitario  with munitario   
                 repla valor     with mvalor   
                 repla empresa   with mtipo
                 repla tiposerv  with mtiposerv
                 sele item
                 skip
               enddo
            endif
         endif 
        enddo
        close item
        restscreen(0,0,24,80,telageral)
        sele notasa   
        go top
        retu(2)
   
   case tecla = 65 .or. tecla = 97   

        telageral := savescreen(0,0,24,80)
        while .t.
         sele notasa 
         legal(0,0,24,79,'Alteracao de Nota Fiscal')
         mnumero := numero         
         @ 1,2  say 'N.F. Numero   :' get mnumero pict '999999'
         read
         if empty(mnumero) .or. lastkey() = 27
            exit
         endif
         mtemp   = str(mnumero,6) + str(mtipo,2)
         seek mtemp  
         if eof()
            mensa(10,'Nota Fiscal nao cadastrada',2)
            loop
         endif
         inicializa()
         @ 2,2  say 'Cliente       :' get mcliente  pict '999999'
         read
         sele clientes
         seek mcliente
         if eof()
            telalocal = savescreen(0,0,24,80)
            do clientes with .t.
            mcliente = codigo
            restscreen(0,0,24,80,telalocal)
         endif
         @ 2,2  say 'Cliente       : ' + strzero(mcliente,6) + ' ' + left(nome,50)
         sele notasa
         @ 1,50 say 'Competencia   :' get minss     pict '99/9999'
         @ 3,2  say 'Emissao       :' get memissao
         @ 3,30 say 'Vencimento :' get mvencto  
         @ 3,60 say 'Impressa   :' get mimpressa pict '!' valid mimpressa $ 'SN'
         read
         sele 100
         use item
         zap
         sele itemadmi
         mtemp = str(mnumero,6) + str(mtipo,2)
         seek mtemp  
         while nota = mnumero .and. empresa = mtipo
          sele itemadmi
          mdescricao = descricao
          munitario  = unitario
          mvalor     = valor
          mtiposerv  = tiposerv
          sele item
          appe blank
          repla descricao with mdescricao
          repla unitario  with munitario
          repla valor     with mvalor
          repla tiposerv  with mtiposerv
          sele itemadmi
          skip
         enddo
         telalocal = savescreen(0,0,24,80)
         sele item
         go top
         declare cabeca[4], estrutura[4]
         cabeca[1]  = 'Detalhamento' ; estrutura[1] = field(1)
         cabeca[2]  = 'Unitario'     ; estrutura[2] = field(2)
         cabeca[3]  = 'Total'        ; estrutura[3] = field(3)
         cabeca[4]  = 'Tipo'         ; estrutura[4] = field(4)
         menu(24,0,' Incluir ',2,1)
         menu(24,9,'Alterar ',1,1)
         menu(24,17,'Excluir ',1,1)
         menu(24,72,'Esc-Sai ',1,3)
         dbedit(2,1,23,78,estrutura,'itensnf','@!',cabeca,.T.)
         restscreen(0,0,24,80,telalocal)
         mtotal = 0
         go top
         while .not. eof()
             mtotal += valor
             skip
         enddo
         @ 16,2 say 'Total da Nota Fiscal ' + space(40) + trans(mtotal,'9,999,999.99') color 'w+/r'
         @ 17,2  say 'Transporte :' get mvaletransp pict '999,999.99'
         @ 17,28 say 'Refeicao :'   get mvalealimen pict '999,999.99'
         @ 17,54 say 'Material :'   get mvmaterial  pict '999,999.99'
         read
         teste(18,2,mretencao1)
         mretencao1 = mretencao
         mvalorret1 = rvalor
         teste(19,2,mretencao2)
         mretencao2 = mretencao
         mvalorret2 = rvalor
         teste(20,2,mretencao3)
         mretencao3 = mretencao
         mvalorret3 = rvalor
         sele notasa
         @ 21,02 say 'IRRF : ' + trans(mirrf,'99.99')
         @ 21,15 say 'CSLL : ' + trans(mcsll,'99.99')
         @ 21,28 say 'COFINS : ' + trans(mcofins,'99.99')
         @ 21,43 say 'PIS : ' + trans(mpis,'99.99')
         mtemp      = mirrf + mcsll + mpis + mcofins
         mfederal   = mtotal * (mtemp / 100)
         @ 21,57 say  trans(mtemp,'99.99') + trans(mfederal,'99,999,999.99')

         mliquido = mtotal - (mvalorret1 + mvalorret2 + mvalorret3 + mfederal)
         @ 22,2 say 'Total liquido        ' + space(40) + trans(mliquido,'9,999,999.99') color 'w+/r'
         @ 23,2  say 'Banco      :' get mbanco pict '99'
         @ 23,20 say 'Material :' get mmaterial pict '!' valid mmaterial $ 'SN'
         read
         sele bancos
         if .not. empty(mbanco)
            seek mbanco
            if eof()
               mensa(10,'Banco nao cadastrado',2)
               loop
            endif
            @ 24,20 say alltrim(nome) + ' Agencia : ' + alltrim(agencia) + ' C/C : ' + alltrim(conta)
         endif
         sele notasa
         @ 23,36 say 'Aliq ISS :' get maliqiss pict '99'
         read
         if mretencao1 # 3 .and. mretencao2 # 3 .and. mretencao3 # 3
            miss = mtotal * (maliqiss/100)
            @ 23,55 say 'I.S.S.  : ' + trans(miss,'999,999.99')
         else
            miss = 0
         endif
         if confirma(4,60,' 1 - Confirma ',' 2 - Cancela  ')
            if inclusao()
               grava()
               repla empresa with mtipo
               unlock
            endif
            sele itemadmi
            go top
            while .not. eof()
             if mnumero = nota .and. mtipo = empresa
                dele 
              endif
              skip
            enddo
            sele item
            go top
            while .not. eof()
             sele item
             mdescricao = descricao
             munitario  = unitario
             mvalor     = valor
             mtiposerv  = tiposerv
             sele itemadmi
             appe blank
             repla nota      with mnumero
             repla descricao with mdescricao
             repla unitario  with munitario
             repla valor     with mvalor
             repla empresa   with mtipo
             repla tiposerv  with mtiposerv
             sele item
             skip
            enddo
         endif 
        enddo
        restscreen(0,0,24,80,telageral)
        sele notasa  
        go top
        retu(2)
 
   case tecla = 78 .or. tecla = 110             // Impressao Nota Fiscal
                                     
        tlocal = savescreen(0,0,24,80)
        do novaadm  
        restscreen(0,0,24,80,tlocal)
        sele notasa
        retu(2)

   
   case tecla = 66 .or. tecla = 98              // Impressao boleto     
                                     
        sele notasa   
        if .not. impressora()
           retu(2)
        endif
        while .t.
         legal(8,4,22,77,'Impressao de Boleto Bancario')
         sele notasa  
         mnota = numero
         @ 10,6 say 'Nota-Fiscal   :' get mnota pict '999999'
         read
         if empty(mnota) .or. lastkey() = 27
            exit
         endif
         mtemp = str(mnota,6) + str(mtipo,2)
         seek mtemp 
         if eof()
            mensa(10,'Nota fiscal nao cadastrada',2)
            loop
         endif
         mcliente = cliente
         mvencto  = vencto
         memissao = emissao
         sele clientes
         seek mcliente
         mnome   = nome
         mretiss = retiss
         if boleto = 'N'
            mensa(10,'Cliente nao utiliza boleto bancario',2)
            mnota+=1
            sele notasa
            skip
            loop
         endif
         sele notasa
         mtotal   = total - (valorret1 + valorret2 + valorret3 + federal)
         if mretiss = 'S'
            mtotal -= iss
         endif
         mmora    = 2.5
         @ 11,6  say 'Cliente       : ' + strzero(mcliente,6) + ' ' + mnome
         @ 12,6  say 'Valor         :' + trans(mtotal,'999,999,999.99')
         @ 12,40 say 'Vencimento    :' get mvencto     
         @ 13,6  say 'Mora          :' get mmora pict '99.99'
         read
         mmora = mtotal * (mmora/100)
         mmora = mmora / 30
         mlinha1 := mlinha2 := space(40)
         @ 14,6  say 'Mora          :' + trans(mmora,'999,999.99')
         @ 15,6  say 'Obs-1         :' get mlinha1 pict '@!'
         @ 16,6  say 'Obs-2         :' get mlinha2 pict '@!'
         read
         if .not. confirma(18,60,' 1 - Confirma ',' 2 - Cancela  ')
            exit
         endif
         sele notasa  
         set devi to printer
         @ prow()+1,50 say mvencto
         @ prow()+3,0  say memissao                                  
         @ prow(),16   say mnota     
         @ prow(),26   say 'NF'                              
         @ prow()+2,50 say mtotal pict '999,999.99'
         @ prow()  ,50 say mtotal pict '999,999.99'
         @ prow()  ,50 say mtotal pict '999,999.99'
         @ prow()+2,0  say 'Cobrar juros de R$ ' + trans(mmora,'9999.99') + ' por dia de atraso.'
         @ prow()+1,0  say mlinha1
         @ prow()+1,0  say mlinha2
         @ prow()+2,2  say ''
         sele clientes
         @ prow()+3,2  say nome                        
         @ prow()+1,2  say endereco
         @ prow()+1,2  say alltrim(cidade) + ' - ' + uf + space(20) + trans(cgc,'@R 99.999.999/9999-99')  
         @ prow()+7,2  say ''
         set devi to screen
         sele notasa
         skip
        enddo
        sele notasa
        skip
        retu(2)
   
   case tecla = 82 .or. tecla = 114                                     
                                     
        telageral := savescreen(0,0,24,80)
        do relatos
        restscreen(0,0,24,80,telageral)
        sele notasa
        go top
        retu(2)

   case tecla = 71 .or. tecla = 103             // GPS
                                     
        sele notasa   
        if .not. impressora()
           retu(2)
        endif
        mvencto  = ctod('')
        mnota = 0        
        while .t.
         legal(8,4,22,77,'Impressao da G.P.S.')
         sele notasa  
         @ 10,6 say 'Nota-Fiscal   :' get mnota pict '999999'
         read
         if empty(mnota) .or. lastkey() = 27
            exit
         endif
         mtemp = str(mnota,6) + str(mtipo,2)
         seek mtemp 
         if eof()
            mensa(10,'Nota fiscal nao cadastrada',2)
            loop
         endif
         mcompet  = right(dtoc(emissao),7)
         mcliente = cliente
         sele clientes
         seek mcliente
         mnome = nome
         @ 11,6  say 'Cliente       : ' + strzero(mcliente,6) + ' ' + mnome
         if gps    = 'N'
            mensa(10,'Cliente nao imprime GPS',2)
            mnota+=1
            sele notasa
            skip
            loop
         endif
         sele notasa
         @ 12,6  say 'Valor         :' + trans(total,'999,999,999.99')
         @ 12,40 say 'Vencimento    :' get mvencto     
         @ 13,6  say 'Competencia   :' get mcompet pict '99/9999'
         read
         if confirma(18,60,' 1 - Confirma ',' 2 - Cancela  ')
            set devi to printer
            @ prow()+1,60 say '2631'   
            @ prow()+2,60 say mcompet                                   
            @ prow()+2,54 say '36.770.857/0001-38'
            @ prow()+1,58 say valorret1 pict '999,999.99'
            @ prow()+1,2  say 'BRASFORT ADMINISTRACAO E SERVICOS LTDA'
            @ prow()+1,2  say 'SAAN QD. 01 No. 635 - BRASILIA'          
            @ prow()+1,2  say left(mnome,40)
            @ prow()  ,2  say left(mnome,40)
            @ prow()  ,2  say left(mnome,40)
            @ prow()+8,2  say 'Retencao da Nota Fiscal : ' + strzero(mnota,6)
            @ prow()+7,2  say ''
            set devi to screen
         endif
         skip
        enddo
        sele notasa
        skip
        retu(2)
   
   
   case tecla = 67 .or. tecla = 99              // Recebimento          

        sele notasa
        while .t.
         mnota = 0
         legal(8,4,22,77,'Recebimento de Nota Fiscal')
         @ 10,6 say 'Nota-Fiscal   :' get mnota pict '999999'
         read
         if empty(mnota) .or. lastkey() = 27
            exit
         endif
         mtemp = str(mnota,6) + str(mtipo,2)
         seek mtemp 
         if eof()
            mensa(10,'Nota fiscal nao cadastrada',2)
            loop
         endif
         mrecebto = recebto
         mcancela = cancela
         mcliente = cliente
         sele clientes
         seek mcliente
         mnome = nome
         sele notasa  
         @ 11,6  say 'Cliente       : ' + strzero(mcliente,6) + ' ' + mnome
         @ 12,6  say 'Valor         :' + trans(total,'999,999,999.99')
         @ 13,6  say 'Recebimento   :' get mrecebto    
         @ 14,6  say 'Cancela       :' get mcancela pict '!' valid mcancela $ 'SN'
         read
         if confirma(18,60,' 1 - Confirma ',' 2 - Cancela  ')
            if alteracao()
               repla recebto with mrecebto
               repla cancela with mcancela
               unlock
            endif
         endif
        enddo
        retu(2)

   case tecla = 80 .or. tecla = 112                                 
                                     
        mnota = 0
        legal(8,4,22,77,'Procura Nota Fiscal')
        @ 10,6 say 'Nota-Fiscal   :' get mnota pict '999999'
        read
        if mnota > 0
           go top
        endif
        locate for mnota = numero
        retu(2)

   case tecla = 69 .or. tecla = 101         // exclusao            

         sele notasa
         legal(16,10,20,70,'Exclusao de Nota Fiscal')
         mnumero := mvalor := 0   
         @ 18,12  say 'N.F. Numero   :' get mnumero pict '999999'
         @ 19,12  say 'Valor         :' get mvalor  pict '999,999,999.99'
         read
         if empty(mnumero) .or. lastkey() = 27
            retu(2)
         endif
         mtemp   = str(mnumero,6) + str(mtipo,2)
         seek mtemp  
         if eof()
            mensa(10,'Nota Fiscal nao cadastrada',2)
            retu(2)
         endif
         if confirma(18,50,' 1 - Confirma ',' 2 - Cancela  ')
            sele notasa
            go top
            dele all for mnumero = numero .and. mtipo = empresa .and. mvalor = total .and. alteracao()
            sele itemadmi
            go top
            dele all for mnumero = nota   .and. mtipo = empresa .and. alteracao()

         endif
         sele notasa
         retu(2)



        retu(2)

     otherwise

        retu(2)

endcase


